package com.platform.common.utils.redis;

import java.util.concurrent.CountDownLatch;

public class RedisRun implements Runnable {

    private CountDownLatch countDownLatch;
    private String deviceId;
    private String cont;
    private RedisUtil redisUtil;
    private boolean flag = true;
    public static String result;

    public RedisRun(CountDownLatch countDownLatch,RedisUtil redisUtil,String deviceId,String cont){
        this.countDownLatch = countDownLatch;
        this.deviceId = deviceId;
        this.cont = cont;
        this.redisUtil = redisUtil;
    }

    @Override
    public void run() {
        int count = 1;
        while(flag){
            try{
                result = (String) redisUtil.hget(deviceId,cont);
                if(result==null){
                    Thread.sleep(3000);
                }else{
                    flag = false;
                    if(countDownLatch!=null){
                        countDownLatch.countDown();
                    }
                }

            }catch (Exception e){
                e.printStackTrace();
                flag = false;
                result = "";
                if(countDownLatch!=null){
                    countDownLatch.countDown();
                }
            }finally {
                count ++;
                if(count>10){
                    flag = false;
                    result = "";
                    if (countDownLatch != null) {
                        countDownLatch.countDown();
                    }
                }
            }
        }
    }
}
